import React, { useEffect } from 'react'
import { Form, Input } from 'antd'
const FormItem = Form.Item

import { FormWidgetProps } from '../FormWidgetRender/WidgetRender'

function index({newSchema, formInstance}: FormWidgetProps) {
  const { fieldName, ui, value = null, props } = newSchema
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { colSpan = 12, offset = 0, ...formItemReset } = ui
  const attribute: any = { size: 'middle', placeholder: '请输入', ...props }

    // vulue值改变时，同步form表单的值
  useEffect(() => {
    if (Object.prototype.toString.call(value) !== '[object Undefined]') {
      // 动态增减表单场景
      // if (config?.condition?.formListKey && config?.condition?.key && Object.prototype.toString.call(key) === '[object Array]') {
      //   const formVal = formRef?.getFieldsValue()
      //   const formListKeyVal = formVal[config.condition.formListKey]
      //   const conditionKey = config?.condition?.key
      //   if (formListKeyVal?.length) {
      //     formListKeyVal?.map((item: any, index: number) => {
      //       if (item && item[conditionKey] && item[conditionKey]?.key && index === key[0]) {
      //         // 依赖的字段有值时
      //         item[key[1]] = value
      //       }
      //     })
      //     formRef?.setFieldsValue({ ...formRef?.getFieldsValue(), [config.formListKey]: formListKeyVal })
      //   }
      // } else {
        formInstance?.setFieldsValue({ ...formInstance?.getFieldsValue(), [fieldName]: value })
      // }
    }
  }, [value])

  return (
    <FormItem
      {...formItemReset}
      name={fieldName}
    >
      <Input {...attribute} />
    </FormItem>
  )
}
export default index
